/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.goloim.model;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 * @author  natx
 */
public class Order {
	
	/**
	 * @uml.property  name="code"
	 */
	private String code;
	/**
	 * @uml.property  name="customer"
	 */
	private String customer;
	private Hashtable<String,OrderLine> htLines;
	
	public void clearOrderLines() {
		htLines.clear();
	}
	
	public boolean hasOrderLines() {
		return !htLines.isEmpty();
	}
	
	public Object[] getOrderLines() {
		Object[] orderLines = new Object[htLines.size()];
		
		Enumeration<OrderLine> enuOrderLines = htLines.elements();
		int i = 0;
		while ( enuOrderLines.hasMoreElements() ) {
			orderLines[i] = enuOrderLines.nextElement();
			i++;
		}
		return orderLines;
	}

	public void add(OrderLine orderLine) {
		if ( htLines.containsKey(orderLine.getProduct().getReference()) ) {
			((OrderLine) htLines.get(orderLine.getProduct().getReference())).setQty(((OrderLine) htLines.get(orderLine.getProduct().getReference())).getQty() + orderLine.getQty());
		} else {
			orderLine.setParent(this);
			htLines.put(orderLine.getProduct().getReference(),orderLine);			
		}
	}
	
	/**
	 * @return  the code
	 * @uml.property  name="code"
	 */
	public String getCode() {
		return code;
	}
	/**
	 * @param code  the code to set
	 * @uml.property  name="code"
	 */
	public void setCode(String code) {
		this.code = code;
	}
	/**
	 * @return  the customer
	 * @uml.property  name="customer"
	 */
	public String getCustomer() {
		return customer;
	}
	/**
	 * @param customer  the customer to set
	 * @uml.property  name="customer"
	 */
	public void setCustomer(String customer) {
		this.customer = customer;
	}
	
	public Order(String code, String customer) {
		this.code = code;
		this.customer = customer;
		this.htLines = new Hashtable<String,OrderLine>();
	}

	public OrderLine getLine(String productReference) throws OrderLineDoesNotExistException {
		if ( !htLines.containsKey(productReference) ) throw new OrderLineDoesNotExistException();
		return htLines.get(productReference);
	}

}
